package com.bwie.controller;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.bwie.pojo.Goods;
import com.bwie.service.GoodsService;
import com.bwie.unit.MessageVo;
import com.bwie.unit.R;
import com.rabbitmq.client.Channel;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.amqp.rabbit.connection.CorrelationData;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.PostConstruct;
import java.io.IOException;
import java.util.UUID;
import java.util.concurrent.TimeUnit;

@RestController
@RequestMapping("user")
public class GoodsController {

    @Autowired
    private GoodsService goodsService;

    @Autowired
    private StringRedisTemplate stringRedisTemplate;

    @Autowired
    private RabbitTemplate rabbitTemplate;


    @PostMapping("PageList")
    public R PageList(@RequestBody Goods goods){
        Page<Goods> goodsPage = new Page<>(goods.getPageNum(), goods.getPageSize());
        Page<Goods> pageList = goodsService.pageList(goodsPage);
        return R.OK(pageList);
    }


    @PostMapping("addGoods")
    @Transactional
    public R addGoods(@RequestBody Goods goods){
          goodsService.save(goods);
        MessageVo messageVo = new MessageVo();
        String msgId = UUID.randomUUID().toString();
        messageVo.setMsgId(msgId);
        messageVo.setMsgBody(String.valueOf(goods));
        stringRedisTemplate.opsForValue().set("goodsList", String.valueOf(goods),15, TimeUnit.MINUTES);
        rabbitTemplate.convertAndSend("directExchange","add", JSON.toJSONString(messageVo));
        return R.OK();
    }

    @RabbitListener(queues = "addQueue")
    public void queue(String msg,Message message, Channel channel) throws IOException {
        MessageVo messageVo = JSON.parseObject(msg, MessageVo.class);
        String msgBody = messageVo.getMsgBody();
        String msgId = messageVo.getMsgId();
        if (!stringRedisTemplate.hasKey(msgId)){
            channel.basicAck(message.getMessageProperties().getDeliveryTag(),true);
            return;
        }
        channel.basicAck(message.getMessageProperties().getDeliveryTag(),true);
    }

    @PostConstruct
    public void huidiao(){
        rabbitTemplate.setConfirmCallback(new RabbitTemplate.ConfirmCallback() {
            @Override
            public void confirm(CorrelationData correlationData, boolean b, String s) {
                if (b){
                    System.out.println("成功");
                }else {
                    System.out.println("失败");
                }
            }
        });
        rabbitTemplate.setReturnCallback(new RabbitTemplate.ReturnCallback() {
            @Override
            public void returnedMessage(Message message, int i, String s, String s1, String s2) {
                rabbitTemplate.convertAndSend(s,s1,message);
                System.out.println("失败");
            }
        });
    }


}
